// pages/login/login.js

const config = require('../../config')
import {
  code
} from '../../utils/result'
var login = function (that) {
  //表单验证
  if (that.data.name == '' || that.data.pwd == '' || that.data.nickName == '' || that.data.idCode == '') {
    wx.showToast({
      title: '请将信息填写完整',
      icon: 'error'
    })
    return
  }
  if (that.data.avatarUrl == '../../images/login.png' || that.data.avatarUrl == '') {
    onChooseAvatar();
    return
  }
  if (that.data.showIdCodeInfo) {
    return
  }
  wx.showLoading()
  let getWxInfo = false
  //先获取UnionID
  wx.login({
    success: (res) => {
      wx.request({
        url: config.host + 'users/code2Session' + '?code=' + res.code,
        method: 'GET',
        success(res) {
          if (res.data.code == code.FAILED) {
            wx.showToast({
              title: '出现错误',
              icon: 'error'
            })
          } else {
            //获取成功后，进行注册
            getWxInfo = true;
            wx.setStorageSync('unionId', res.data.data)
            console.log(res.data.data)
            that.data.unionId = res.data.data
            wx.request({
              url: config.host + 'users/register',
              method: 'POST',
              data: {
                userAvatar: that.data.avatarUrl,
                userName: that.data.name,
                userNickName: that.data.nickName,
                userPwd: that.data.pwd,
                unionId: that.data.unionId,
                userIdCode: that.data.idCode,
                userSex: that.data.userSex
              },
              fail(e) {
                wx.hideLoading()
                wx.showToast({
                  title: '网络出现错误',
                  icon: 'error'
                })
              },
              success: function (res) {
                wx.hideLoading();
                //后端返回成功
                if (res.data.code == code.SUCCESS) {
                  wx.switchTab({
                    url: '../index/index'
                  })
                }
                //后端返回失败
                else {
                  wx.showToast({
                    title: '用户名重复了',
                    icon: 'error'
                  })
                }
              }
            })
          }
        }
      })
    },
  })

}

Page({
  data: {
    name: '',
    pwd: '',
    avatarUrl: '../../images/login.png',
    avatarSelectInfo: '请选择头像',
    nickName: '',
    unionId: '',
    userInfo: null,
    idCode: '',
    showIdCodeInfo: false,
    userSex: 0,
    sexUrl: '../../images/icon-male.png',
    showSexIcon: false
  },

  checkIdCode(e) {
    let _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
    if (!_IDRe18.test(this.data.idCode)) {
      this.setData({
        showIdCodeInfo: true
      })
    } else {

      let sexBit = this.data.idCode[this.data.idCode.length - 2]
      if (parseInt(sexBit) % 2 == 0) {
        this.data.userSex = 1
      } else {
        this.data.userSex = 0
      }
      this.setData({
        showIdCodeInfo: false,
        showSexIcon: true,
        sexUrl: this.data.userSex == 0 ? '../../images/icon-male.png' : '../../images/icon-female.png'
      })
    }
  },
  unrealizeNow(){
    wx.showToast({
      title: '暂时不支持！',
      icon: 'error',
      mask: true
    })
  },
  onChooseAvatar: function (e) {
    this.setData({
      avatarUrl: e.detail.avatarUrl,
      avatarSelectInfo: ''
    })
  },

  bindIdCodeInput(e) {
    console.log(e)
    this.setData({
      idCode: e.detail.value
    })
  },

  bindNameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
  },

  bindNickNameInput: function (e) {
    this.setData({
      nickName: e.detail.value
    })
  },
  bindPwdInput: function (e) {
    this.setData({
      pwd: e.detail.value
    })
  },

  bindInputLogin: function (e) {
    login(this)
  },

  formLogin: function (e) {
    login(this)
  },
})